package com.xds.gulimall.product.web;

import com.xds.gulimall.product.entity.CategoryEntity;
import com.xds.gulimall.product.service.CategoryService;
import com.xds.gulimall.product.vo.html.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author : baixu
 * @date : 16:36 2021/12/1
 */
@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    RedissonClient  redisson;
    @Autowired
    StringRedisTemplate redisTemplate;
    @GetMapping({"/","/index.html"})
    public String IndexPage(Model model){
        //TODO 查询所有一级分类
    List<CategoryEntity> categoryEntities= categoryService.getLevel1Categorys();
    model.addAttribute("categorys",categoryEntities);
        return "index";
    }
    @GetMapping("index/catalog.json")
    @ResponseBody
    public  Map<String, List<Catelog2Vo>> getCatelogJson(){
        Map<String, List<Catelog2Vo>> map=    categoryService.getCatelogJson();
        return map;
    }
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        //获取一把锁
        RLock lock = redisson.getLock("my-lock");
        //2.加锁//阻瑟等待
//        lock.lock();
        lock.lock(10, TimeUnit.SECONDS);//10秒钟自动解锁 自动解锁时间一定要大于业务的执行时间 否则报错  attempt to unlock lock, not locked by current thread by node id
        try {
            System.out.println("加锁成功 执行业务"+Thread.currentThread().getId());
            Thread.sleep(30000);
        }catch (Exception e){

        }
        finally {
            //3.解锁
            System.out.println("释放锁"+Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }
    @ResponseBody
    @GetMapping("/write")
    public String writeValue(){

        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock writeLock= readWriteLock.writeLock(); ;
        try {
            //改数据加写锁
            writeLock.lock();
            s=UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writevalue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            //释放写锁
            writeLock.unlock();
        }
        return s;
    }
    @ResponseBody
    @GetMapping("/read")
    public String read(){
        RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
        RLock readLock = readWriteLock.readLock();
        String s = "";
        //加读锁
        readLock.lock();
        try {
           s= redisTemplate.opsForValue().get("writevalue");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //释放锁
            readLock.unlock();
        }
        return s;
    }
}
